package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ErrorMessageConstant;
import com.xbongbong.saas.domain.entity.ClueRuleEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author long
 * @version v1.0
 * @date 2020/12/2 14:24
 * @since v1.0
 */
public class OperatePrePrincipalClueRuleValue implements IOperateClueRuleValue {

    private static final Logger LOG = LoggerFactory.getLogger(OperatePrePrincipalClueRuleValue.class);

    @Override
    public void verify(String ruleValue) throws XbbException {
        JSONArray ruleArray = null;
        try {
            ruleArray = JSON.parseArray(ruleValue);
        } catch (Exception e) {
            // ignore
        }

        if (ruleArray == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
        }

        for (Object ruleObject : ruleArray) {
            JSONObject ruleJson;
            Integer value;
            JSONArray users;
            try {
                ruleJson = (JSONObject) ruleObject;
                value = ruleJson.getInteger("value");
                users = ruleJson.getJSONArray("users");
            } catch (Exception e) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
            }
            if (value == null || value < 1) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.DAYS_GREATER_ZERO);
            }
            if (users != null && users.size() == 0) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.SPECIAL_STAFF_NOT_BE_EMPTY);
            }
        }
    }

    @Override
    public Object toObject(ClueRuleEntity rule) {
        if (rule == null) {
            return null;
        }
        Object retObject;

        try {
            retObject = JSON.parseArray(rule.getRuleValue());
        } catch (Exception e) {
            retObject = null;
            LOG.error("corpid:" + rule.getCorpid() + " ruleId:" + rule.getId() + "------json格式错误\n " + rule.getRuleValue());
        }

        return retObject;
    }

    @Override
    public List<String> getAllSpecialUser(Object ruleObject) {
        List<String> allSpecialUsers = new ArrayList<>();

        Set<String> allSpecialUserSet = new HashSet<>();

        JSONArray ruleObjectArray = null;
        try {
            ruleObjectArray = (JSONArray) ruleObject;
        } catch (Exception e) {
            // ignore
        }

        if (ruleObjectArray == null) {
            return allSpecialUsers;
        }

        for (Object ruleObj : ruleObjectArray) {
            JSONObject ruleJson = (JSONObject) ruleObj;
            List<String> users = JSON.parseObject(ruleJson.getString("user"),
                    new TypeReference<List<String>>() {
                    });
            if (users != null) {
                allSpecialUserSet.addAll(users);
            }
        }
        allSpecialUsers.addAll(allSpecialUserSet);
        return allSpecialUsers;
    }

    /**
     * 获取规则配置中的标签Id
     *
     * @param ruleObject
     * @return
     */
    @Override
    public List<Long> getClueLabelIds(Object ruleObject) {
        List<Long> clueLabelIds = new ArrayList<>();

        Set<Long> clueLabelIdSet = new HashSet<>();

        JSONObject labelObject = null;
        try {
            labelObject = JSONObject.parseObject(ruleObject.toString());
        } catch (Exception e) {
            // ignore
        }

        if (labelObject == null) {
            return clueLabelIds;
        }

        List<Long> labelIds = labelObject.getObject("label", List.class);
        if (labelIds != null) {
            clueLabelIdSet.addAll(labelIds);
        }
        clueLabelIds.addAll(clueLabelIdSet);
        return clueLabelIds;
    }

    @Override
    public Integer getStatus(ClueRuleEntity clueRule) {
        if (clueRule != null) {
            return clueRule.getStatus();
        }
        return null;
    }
    
    @Override
    public JSONArray getClueStatus(ClueRuleEntity clueRule) {
        return null;
    }

    @Override
    public Integer getType(ClueRuleEntity clueRule) {
        return null;
    }

    @Override
    public JSONArray getRestrict(ClueRuleEntity clueRule) {
        JSONArray retArray = (JSONArray) toObject(clueRule);
        return retArray;
    }
}
